package model.dba;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.IRHorarioCursada;
import model.Usuario;

public class IRHorarioCursadaDAO extends JDBC{
	private static IRHorarioCursadaDAO iRHorarioCursadaDAO;

	private IRHorarioCursadaDAO() {
		super();
	}

	public static IRHorarioCursadaDAO getInstance() {
		if (iRHorarioCursadaDAO == null) {
			iRHorarioCursadaDAO = new IRHorarioCursadaDAO();
		}
		return iRHorarioCursadaDAO;
	}

	public int add(IRHorarioCursada iRhorarioCursada) {
		try {

			String queryString = "INSERT INTO iRhorarioCursada(idHorarioC,idCursadaC)" + " VALUES(?,?) ";
			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setLong(1, iRhorarioCursada.getIdHorario());
			preparedStatement.setLong(2,iRhorarioCursada.getidCursada());

			preparedStatement.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return getLastId();

	}

	// En un futuro se puede poner nombre

	public boolean update(IRHorarioCursada iRhorarioCursada) {
		boolean state = false;
		try {
			String queryString = "UPDATE iRhorarioCursada SET idHorarioC=?, idCursadaC=? WHERE idHorarioC=?";

			preparedStatement = connection.prepareStatement(queryString);

			preparedStatement.setLong(1, iRhorarioCursada.getIdHorario());
			preparedStatement.setLong(2, iRhorarioCursada.getidCursada());
			preparedStatement.setLong(3, iRhorarioCursada.getIdHorario());


			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			}

			catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();

			}
		}
		return state;
	}

	public boolean delete(Integer idHorarioC) {
		boolean state = false;
		try {
			String queryString = "DELETE FROM iRhorarioCursada WHERE idHorarioC=?";

			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setInt(1, idHorarioC);
			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return state;

	}

	public IRHorarioCursada get(Integer idHorario) {

		IRHorarioCursada iRhorarioCursada = new IRHorarioCursada();
		try {
			String queryString = "SELECT * FROM iRhorarioCursada where idHorarioC = '" + idHorario + "';";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				iRhorarioCursada = populate();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return iRhorarioCursada;
	}

	// En un futuro se puede poner nombre

	public IRHorarioCursada find(Integer idCursada) {

		IRHorarioCursada iRhorarioCursada = new IRHorarioCursada();
		try {
			String queryString = "SELECT * FROM iRhorarioCursada where idCursadaC ='" + idCursada + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				iRhorarioCursada = populate();

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return iRhorarioCursada;
	}

	public int getLastId() {
		int ret = 0;
		try {
			String queryString = "SELECT idHorarioC FROM iRhorarioCursada order by idHorarioC DESC LIMIT 1  ";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				ret = resultSet.getInt("idHorarioC");

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return ret;

	}

	public List<IRHorarioCursada> getAll() {
		List<IRHorarioCursada> iRhorarioCursada = new ArrayList<IRHorarioCursada>();

		try {
			String queryString = "SELECT * FROM iRhorarioCursada;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				iRhorarioCursada.add(populate());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return iRhorarioCursada;
	}
	
	public List<IRHorarioCursada> findAll(Integer idCursadaC) {

		List<IRHorarioCursada> areaList = new ArrayList<IRHorarioCursada>();
		try {
			String queryString = "SELECT * FROM iRhorarioCursada where idCursadaC  = '" + idCursadaC + "' ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				areaList.add(populate());

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return areaList;
	}

	private IRHorarioCursada populate() throws SQLException {

		IRHorarioCursada iRhorarioCursada = new IRHorarioCursada();
		iRhorarioCursada.setIdhorario((int) resultSet.getLong("idHorarioC"));
		iRhorarioCursada.setidCursada((int) resultSet.getLong("idCursadaC"));
		return iRhorarioCursada;

	}

}
